﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using Module.Core.DAO;
using Module.Core.EntitiesInfo;
using Module.Core.Common;
using Module.Common.Models;

namespace Module.Core.BusinessLogic
{
    public class ConfigKeywordBL
    {

        #region method
        /// <summary>
        /// Saves a record to the ConfigKeyword table.
        /// </summary>
        public static int Insert(ConfigKeywordInfo configKeywordInfo)
        {
            try
            {
                return new ConfigKeywordDAO().Insert(configKeywordInfo);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        /// <summary>
        /// Saves a record to the ConfigKeyword table.
        /// </summary>
        public static void Update(ConfigKeywordInfo configKeywordInfo)
        {
            try
            {
                new ConfigKeywordDAO().Update(configKeywordInfo);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        /// <summary>
        /// Delete a record from the ConfigKeyword table.
        /// </summary>
        public static void Delete(int configId)
        {
            try
            {
                new ConfigKeywordDAO().Delete(configId);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        /// <summary>
        /// Selects a single record from the ConfigKeyword table.
        /// </summary>
        public static ConfigKeywordInfo Select(int configId)
        {
            try
            {
                return new ConfigKeywordDAO().Select(configId);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        /// <summary>
        /// Selects all records from the ConfigKeyword table.
        /// </summary>
        public static CHRTList<ConfigKeywordInfo> SelectAll()
        {
            try
            {
                return new ConfigKeywordDAO().SelectAll();
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        public static void XMLConfig(string fileNameXML, string pathWriteXML, int lang)
        {
            try
            {
                new ConfigKeywordDAO().XMLConfig(fileNameXML, pathWriteXML, lang);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static string GetValueKeyWord(string key, int lang)
        {
            string value_key = "";         
            string path = ComomFunctions.GetUrlXML(Const.VN, Const.folderKeyConfig, Const.Keyconfig);
            XmlTextReader xml = new XmlTextReader(path);
            DataSet ds = new DataSet();

            try
            {
                ds.ReadXml(xml);
                xml.Close();
            }
            catch (Exception ex)
            {
                xml.Close();
                // HttpContext.Current.Response.Write(ex.Message);
            }
            finally
            {
                xml.Close();
            }

            if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];

                if (dt.Rows.Count > 0)
                {
                    DataRow[] dr = dt.Select("ConfigKey = '" + key + "'");
                    if (dr.Length > 0 && lang == 1)
                    {

                        value_key = dr[0]["ConfigValue_VN"].ToString();
                    }
                    else if (dr.Length > 0 && lang == 2)
                    {

                        value_key = dr[0]["ConfigValue_Eng"].ToString();
                    }
                }
                else
                {
                    return "";
                }
            }
            if (value_key != "" && value_key != null)               
                return value_key;
            return null;
        }

        #endregion

    }
}
    